<?php
/**
 * This is a quick& dirty bootstrap file for the whole application.
 * It should be redone, building a more robust dispatcher.
 */

// Load the framework
require( 'includes/configuration/prepend.inc.php' );

// Before anything else, if anything is set in the $_SESSION['qa_user'] var,
// make usre it is a valid user object, and reset it if not
if( isset( $_SESSION['qa_user'] ) ) {
    // $_SESSION['qa_user'] = unserialize( $_SESSION['qa_user'] );
    if( !$_SESSION['qa_user'] instanceof User )
        unset( $_SESSION['qa_user'] );
}

// Get the controller/action/params from the RequestURI
$strRequest = strtolower( $_SERVER['REQUEST_URI'] );

// Remove the subdirectory, if there is one
if( substr( $strRequest, 0, strlen(__SUBDIRECTORY__) ) == strtolower(__SUBDIRECTORY__) )
    $strRequest = substr( $strRequest, strlen(__SUBDIRECTORY__) );

// Remove the leading '/'
if( substr( $strRequest, 0, 1 ) == '/' ) $strRequest = substr( $strRequest, 1 );

// Put it in an array
$arrRequest = explode( '/', $strRequest );

// Path where the actual scripts are
$strModulePath = __DOCROOT__ . __SUBDIRECTORY__ .'/app';

// Peek into the reguest to see if we're requesting an admin module
if( (count($arrRequest) > 0) && (strtolower($arrRequest[0]) == 'admin') ) {
    $strModulePath .= '/admin';
    array_shift( $arrRequest );
    $adminModule = true;
}
else $adminModule = false;
    
// Get the first item (Controller name) from the request. Default it to 'index'
if( $strController = array_shift( $arrRequest ) ) {
    // if it is numeric, push it back to the array, and assume 'index'
    if( intval($strController) > 0 ) {
        array_unshift( $arrRequest, $strController );
        $strController = 'index';
        $strAction = 'index';
    }
    // Get the next item (Action name) from the request. Default it to 'index'
    elseif( $strAction = array_shift( $arrRequest ) ) {
        // if it is numeric, push it back to the array, and assume 'index'
        if( intval($strAction) > 0 ) {
            array_unshift( $arrRequest, $strAction );
            $strAction = 'index';
        }
    }
    else $strAction = 'index';
}
else {
    $strController = 'index';
    $strAction = 'index';
}

// TODO: The remainder of $strRequest are parameters for the Controller/Action.
// This seems to work, but I'm not convinced it is the correct way of doing this
QApplication::$PathInfo = '/' . implode( '/', $arrRequest );

// Define the controller and view filenames
$strFilename = $strModulePath .'/'. $strController .'/'. $strAction .'.php';
$strTemplate = $strModulePath .'/'. $strController .'/'. $strAction .'.tpl.php';

/*
echo 'Controller: '. $strController .'<br>';
echo 'Action: '. $strAction .'<br>';
echo 'Params: '. implode( '/', $arrRequest ) .'<br>';
echo 'Controller File: '. $strFilename .'<br>';
echo 'View File: '. $strTemplate .'<br>';
exit;
*/

// Catch an error if the Controller/Action file does not exist
if( !is_file( $strFilename ) ) {
    $strController = 'error';
    $strAction = 'error404';
    $strFilename = __DOCROOT__ . __SUBDIRECTORY__ .'/app/error/error404.php';
    $strTemplate = __DOCROOT__ . __SUBDIRECTORY__ .'/app/error/error404.tpl.php';
}

// Check if user is allowed this Module/Controller/Action combo
elseif( !User::hasAccess( $strController, $strAction, $adminModule ) ) {
    $strController = 'error';
    $strAction = 'error403';
    $strFilename = __DOCROOT__ . __SUBDIRECTORY__ .'/app/error/error403.php';
    $strTemplate = __DOCROOT__ . __SUBDIRECTORY__ .'/app/error/error403.tpl.php';
}

// Include the file
require( $strFilename );

// If the view exists, run the form in its view
if( is_file( $strTemplate ) )
    call_user_func( array( $strAction, 'run' ), $strAction, $strTemplate );

// Otherwise, just we trust the Controller/Action file is handling whatever it needs to do itself
?>